<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 用户控制器
 * @author derek <693472824@qq.com>
 */
class UserController extends BaseController {

    /**
     * 获取用户列表
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $param = I('param');
        
        $map = array();
        
        if(!empty($param)){
        
            $map['account|username'] = array('like', '%'.(string)$param.'%');
        }
        
        $userModel = M('User');
        $list = $this->lists($userModel, $map);
        
        $this->assign('param', $param);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 增加或修改用户
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
    
        $userID = I('user_id');
    
        if (!empty($userID)) {
    
            $userModel = $this->find($userID);
    
            if (empty($userModel)) {
                $this->error('获取数据失败', U('index'));
            }else{
    
                $this->assign('userModel', $userModel);
            }
        }
    
        $this->display();
    }

    /**
     * 保存用户信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
    
        $userLogic = D('User', 'Logic');
        $result = $userLogic->saveAction();
    
        //1:只新增用户；2：新增用户后下单
        $smtType = I('smt_type') ? : '1';
        
        if($smtType == '2' && $result !== false){
            
            $this->redirect('Orders/editAction', array('user_id'=>$result));
        }else if ($result === false) {
            $this->error('操作失败' . $userLogic->getError());
        } else {
            $this->success('操作成功', U('index'));
        }
    }
    
    /**
     * 用户维修地址
     */
    public function addressIndex(){
        
        $userID = I('user_id');
        
        if(empty($userID)){
            $this->error('参数错误', U('index'));
        }
        
        $param = I('param');
        
        $map = array();
        
        if(!empty($param)){
            $map['contact|address'] = array('like', '%'.$param.'%');
        }
        
        $map['user_id'] = $userID;
        
        $userModel = M('User')->find($userID);
        
        $addressModel = M('UserAddress');
        $list = $this->lists($addressModel, $map);
        
        $available = AdminLogic::getSite();
        
        //当前用户可管理站点
        if(!empty($available)){
            $map['site_id'] = $available;
        }
        
        $siteList = M('Site')->where($map)->getField('site_id,site_name,address');
        
        $this->assign('siteList', $siteList);
        $this->assign('param', $param);
        $this->assign('userModel', $userModel);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 新增或修改地址
     */
    public function editAddressAction(){
        
        $userID = I('user_id');
        $addressID = I('address_id');
        
        if(empty($userID)){
            $this->error('参数错误');
        }else{
            $userModel = M('User')->find($userID);
            $this->assign('userModel', $userModel);
        }
        
        if (!empty($addressID)) {
        
            $addressModel = M('UserAddress')->find($addressID);
        
            if (empty($addressModel)) {
                $this->error('获取数据失败', U('index'));
            }else{
        
                $this->assign('addressModel', $addressModel);
            }
        }
        
        $this->display();
    }

    /**
     * 保存用户维修地址信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveAddressAction() {
    
        $userLogic = D('User', 'Logic');
        $result = $userLogic->saveAddressAction();
    
        //1:只新增用户维修地址；2：新增用户维修地址后下单
        $smtType = I('smt_type') ? : '1';
        $userID = I('user_id');
    
        if($smtType == '2' && $result !== false){
    
            $this->redirect('Orders/editAction', array('user_id'=>$userID, 'address_id'=>$result));
        }else if ($result === false) {
            $this->error('操作失败' . $workerLogic->getError());
        } else {
            $this->success('操作成功', U('addressIndex', 'user_id='.$userID));
        }
    }
    

    /**
     * 用户优惠券
     */
    public function couponIndex(){
    
        $userID = I('user_id');
    
        if(empty($userID)){
            $this->error('参数错误', U('index'));
        }
    
        $userModel = M('User')->find($userID);
    
        $map['user_id'] = $userID;
        
        $couponModel = M('UserCoupon');
        $list = $this->lists($couponModel, $map, 'status,create_time DESC');
    
        $this->assign('userModel', $userModel);
        $this->assign('list', $list);
        $this->display();
    }
}